package com.ebelter.seneo.ui.fragments.finpsw;


import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.ebelter.btcomlib.models.https.NetUtils;
import com.ebelter.btcomlib.models.https.interfaces.HttpResponse;
import com.ebelter.btcomlib.models.https.responses.getEmailValidateCodeResponse;
import com.ebelter.btcomlib.utils.ToastUtil;
import com.ebelter.btcomlib.utils.VerifyUtils;
import com.ebelter.btcomlib.utils.ViewUtils;
import com.ebelter.btcomlib.views.CountDownTextView;
import com.ebelter.btcomlib.views.dialogs.LoadingDialog;
import com.ebelter.seneo.R;
import com.ebelter.seneo.ui.activitys.SeneoModifyPswActivity;
import com.ebelter.seneo.ui.fragments.BaseFragment;


//获取邮箱验证码地址
public class GetEmailCode_Fragment extends BaseFragment {

    EditText getEmailCode_email_ed;
    EditText getEmailCode_emailcode_ed;
    CountDownTextView countDownTv;
    @Override
    protected int setLayoutResouceId() {
        return R.layout.getemailcode_fly;
    }

    @Override
    protected void initView() {
        getEmailCode_email_ed=findViewById(R.id.getEmailCode_email_ed);
        getEmailCode_emailcode_ed=findViewById(R.id.getEmailCode_emailcode_ed);
        countDownTv=findViewById(R.id.countDownTv);
        countDownTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendEmailVC();
            }
        });


        if (getActivity()!=null){
            String email = ((SeneoModifyPswActivity)getActivity()).getEmail();
            if (!TextUtils.isEmpty(email)){
                getEmailCode_email_ed.setText(email);
                getEmailCode_email_ed.setEnabled(false);
            }
        }
    }


    //发送邮箱验证码
    private void sendEmailVC() {
        String mail=getEmailCode_email_ed.getText().toString().trim();
        if (TextUtils.isEmpty(mail)){
            ToastUtil.show(R.string.Email_must_not_be_empty);
            return;
        }

        if (!VerifyUtils.emailFormat(mail)){
            ToastUtil.show(R.string.Incorrect_email_format);
            return;
        }
        if (!NetUtils.available()){
            ToastUtil.show(R.string.Network_error);
            return;
        }
        showLoadingDialog("");
        NetUtils.getInstance().getEmailValidateCodeRequest(getActivity(), mail, 2, new HttpResponse<getEmailValidateCodeResponse>() {
            @Override
            public void result(boolean isRequestSuccess, String requestDes, getEmailValidateCodeResponse getEmailValidateCodeResponse,String s) {
                dissLoadingDialog();
                if (isRequestSuccess){
                    if (getEmailValidateCodeResponse.resultCode==0){
                        ToastUtil.show(R.string.The_email_has_been_sent);
                        if (countDownTv!=null){countDownTv.click();}

                        if (getActivity() != null) {
                            ((SeneoModifyPswActivity)getActivity()).setNextBtEnabled(true);
                        }


                    }else if (getEmailValidateCodeResponse.resultCode==104){//邮箱没有注册
                        ToastUtil.show(R.string.Email_is_not_registered);
                    }else if (getEmailValidateCodeResponse.resultCode==110){//服务器异常
                        ToastUtil.show(R.string.An_exception_occurred_on_the_server);
                    }else {
                        ToastUtil.show(R.string.Failed_to_send_mail);
                    }
                }else {
                    ToastUtil.show(R.string.Network_connection_error);
                }
            }
        });
    }

    public String getEmail() {
        return getEmailCode_email_ed.getText().toString().trim();
    }

    public String getEmailCode() {
        return getEmailCode_emailcode_ed.getText().toString().trim();
    }

    private LoadingDialog loadingDialog;
    public void showLoadingDialog(String tip){
        if (loadingDialog==null){
            loadingDialog=new LoadingDialog(getActivity());
        }
        if (!loadingDialog.isShowing()){
            loadingDialog.show();
        }
        loadingDialog.setTip(tip);

    }

    public void dissLoadingDialog(){
        if (loadingDialog!=null&&loadingDialog.isShowing()){
            loadingDialog.cancel();
        }
    }
}
